package org.report.util;

/**
 * 数字处理工具
 * @author Administrator
 * @describe 说明
 * @date 2024/5/8 0008 11:54
 */
public class NumberUtils {
    private static final String[] CHINESE_NUMBERS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] CHINESE_UNITS = {"", "十", "百", "千", "万", "亿"};

    /**
     * 转换为汉字为
     *
     * @param number
     * @return
     */
    public static String convertToChinese(int number) {
        if (number == 0) {
            return CHINESE_NUMBERS[0];
        }

        StringBuilder result = new StringBuilder();
        int unitIndex = 0;
        boolean needZero = false;

        while (number > 0 && unitIndex < CHINESE_UNITS.length) {
            int currentNumber = number % 10;
            if (currentNumber == 0) {
                needZero = true;
            } else {
                if (needZero) {
                    result.insert(0, CHINESE_NUMBERS[0]);
                    needZero = false;
                }
                result.insert(0, CHINESE_NUMBERS[currentNumber] + CHINESE_UNITS[unitIndex]);
            }
            unitIndex++;
            number /= 10;
        }
        return result.toString();
    }
}
